package com.zhc.dejingapi.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.zhc.dejingapi.entity.vo.DateAndNumVo;
import com.zhc.dejingapi.entity.vo.ProductNumVo;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.param.QingCommonBackTwoResult;
import com.zhc.dejingapi.pojo.param.QingReportQueryParam;
import com.zhc.dejingapi.service.upload.IQingFlowReportDataGetService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/qingflowReport")
@CrossOrigin
public class  QingFlowReportDataFetchController {
    private final JdbcTemplate mysqlJdbcTemplate;
  private final IQingFlowReportDataGetService qingFlowReportDataGetService;
    public QingFlowReportDataFetchController(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate,
                                             IQingFlowReportDataGetService qingFlowReportDataGetService1) {
        this.mysqlJdbcTemplate = mysqlJdbcTemplate;
        this.qingFlowReportDataGetService = qingFlowReportDataGetService1;
    }
    /**
     * @Author: 
     * @Description:  获取表格数据
     * @Date: 2024/8/15 14:45
 * @param apiKey
 * @param param 
 * @return com.zhc.dejingapi.pojo.param.QingCommonBackResult
     */
    
    @PostMapping("/tableChart")//获取表格数据
    public QingCommonBackResult GetTableReportData(@RequestHeader(name = "apiKey") String apiKey,
                                                   @RequestBody QingReportQueryParam param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        try{

            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }

            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";
            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }

            return qingFlowReportDataGetService.GetTableChartData(param);

        }
        catch (Exception e){

            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }

    
    /**
     * @Author: zhc
     * @Description:  获取指标的值
     * @Date: 2024/8/15 14:45
 * @param apiKey
 * @param param 
 * @return com.zhc.dejingapi.pojo.param.QingCommonBackResult
     */
    
    @PostMapping("/keyChart")
    public QingCommonBackResult GetKeyReportData(@RequestHeader(name = "apiKey") String apiKey,
                                                   @RequestBody QingReportQueryParam param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        try{
            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }
            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";
            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }
            return qingFlowReportDataGetService.GetKPIChartData(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());
        }

        return commonBackResult;
    }


    /**
     * @Author: zhc
     * @Description:  获取饼图的值
     * @Date: 2024/8/15 16:13
     * @param apiKey
     * @param param
     * @return com.zhc.dejingapi.pojo.param.QingCommonBackResult
     */
    
    @PostMapping("/pieChart")
    public QingCommonBackResult GetPieReportData(@RequestHeader(name = "apiKey") String apiKey,
                                                 @RequestBody QingReportQueryParam param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        try{

            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }

            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";
            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }

            return qingFlowReportDataGetService.GetPieChartData(param);

        }
        catch (Exception e){

            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;
    }

    @PostMapping("/tableChart1")//获取表格数据
    public QingCommonBackTwoResult GetTableReportData1(@RequestHeader(name = "apiKey") String apiKey,
                                                       @RequestBody QingReportQueryParam param){
        QingCommonBackTwoResult commonBackResult2=new QingCommonBackTwoResult();
        try{

            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }

            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";
            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }

            return qingFlowReportDataGetService.GetTableChartData1(param);

        }
        catch (Exception e){

            e.printStackTrace();
            commonBackResult2.setErrCode(-10012);
            commonBackResult2.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult2;
    }

    @PostMapping("/getProductNum")
    public ProductNumVo getProductNum(){
        List<String> dates = new ArrayList<>();
        List<Double> goodNums = new ArrayList<>();
        List<Double> badNums = new ArrayList<>();
        QingReportQueryParam good = new QingReportQueryParam();
        good.setReportKey("862tpbnc0g02");
        List<DateAndNumVo> goods = qingFlowReportDataGetService.GetPieChartData1(good);


        QingReportQueryParam bad = new QingReportQueryParam();
        good.setReportKey("862tfgr00g02");
        List<DateAndNumVo> bads = qingFlowReportDataGetService.GetPieChartData1(good);
        Date date = DateUtil.date();
        for(int i=0;i<5;i++){
            DateTime newDate = DateUtil.offsetDay(date, -i);
            String format = DateUtil.format(newDate, "yyyy年MM月dd日");
            String format1 = DateUtil.format(newDate, "yyyy-MM-dd");
            boolean goodFlag= false;
            for (DateAndNumVo g:goods) {
                if (g.getDate().equals(format)){
                    goodFlag=true;
                    goodNums.add(g.getNum());
                    break;
                }else {
                    goodFlag=false;
                }
            }

            if(!goodFlag){
                goodNums.add(0.0);
            }

            boolean badFlag= false;
            for (DateAndNumVo b:bads) {
                if (b.getDate().equals(format)){
                    badFlag=true;
                    badNums.add(b.getNum());
                    break;
                }else {
                    badFlag=false;
                }
            }

            if(!badFlag){
                badNums.add(0.0);
            }

            dates.add(format1);
        }

        ProductNumVo productNumVo = new ProductNumVo();
        productNumVo.setDates(dates);
        productNumVo.setGoodNums(goodNums);
        productNumVo.setBadNums(badNums);
        return productNumVo;
    }
}
